package com.java110.mall.cart.bmo.storeOrderCartUp.impl;

import com.java110.mall.cart.bmo.storeOrderCartUp.IGetStoreOrderCartUpBMO;
import com.java110.dto.file.FileRelDto;
import com.java110.intf.cart.IStoreOrderCartUpInnerServiceSMO;
import com.java110.intf.system.IFileRelInnerServiceSMO;
import com.java110.utils.cache.MappingCache;
import com.java110.utils.constant.MappingConstant;
import com.java110.utils.util.StringUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import com.java110.dto.storeOrder.StoreOrderCartUpDto;

import java.util.ArrayList;
import java.util.List;

@Service("getStoreOrderCartUpBMOImpl")
public class GetStoreOrderCartUpBMOImpl implements IGetStoreOrderCartUpBMO {

    @Autowired
    private IStoreOrderCartUpInnerServiceSMO storeOrderCartUpInnerServiceSMOImpl;
    @Autowired
    private IFileRelInnerServiceSMO fileRelInnerServiceSMOImpl;
    /**
     *
     *
     * @param  storeOrderCartUpDto
     * @return 订单服务能够接受的报文
     */
    public ResponseEntity<String> get(StoreOrderCartUpDto storeOrderCartUpDto) {


        int count = storeOrderCartUpInnerServiceSMOImpl.queryStoreOrderCartUpsCount(storeOrderCartUpDto);

        List<StoreOrderCartUpDto> storeOrderCartUpDtos = null;
        if (count > 0) {
            storeOrderCartUpDtos = storeOrderCartUpInnerServiceSMOImpl.queryStoreOrderCartUps(storeOrderCartUpDto);
            storeOrderCartUpDtos = freshProductDtos(storeOrderCartUpDtos);

        } else {
            storeOrderCartUpDtos = new ArrayList<>();
        }

        ResultVo resultVo = new ResultVo((int) Math.ceil((double) count / (double) storeOrderCartUpDto.getRow()), count, storeOrderCartUpDtos);

        ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);

        return responseEntity;
    }
    private List<StoreOrderCartUpDto> freshProductDtos(List<StoreOrderCartUpDto> storeOrderCartUpDtos) {

        if (storeOrderCartUpDtos == null || storeOrderCartUpDtos.size() < 1) {
            return storeOrderCartUpDtos;
        }
        //输入
        List<String> productIds = new ArrayList<>();

        for (StoreOrderCartUpDto tmpStoreOrderCartUpDto : storeOrderCartUpDtos) {
            productIds.add(tmpStoreOrderCartUpDto.getProductId());
        }

        FileRelDto fileRelDto = new FileRelDto();
        fileRelDto.setObjIds(productIds.toArray(new String[productIds.size()]));
        fileRelDto.setRelTypeCd(FileRelDto.REL_TYPE_CD_GOODS_COVER);
        List<FileRelDto> fileRelDtos = fileRelInnerServiceSMOImpl.queryFileRels(fileRelDto);

        //刷入图片信息
        String url = null;
        String imgUrl = MappingCache.getValue(MappingConstant.FILE_DOMAIN,"IMG_PATH");
        imgUrl += (!StringUtil.isEmpty(imgUrl) && imgUrl.endsWith("/") ? "" : "/");

        for (StoreOrderCartUpDto tmpStoreOrderCartUpDto : storeOrderCartUpDtos) {
            for (FileRelDto tmpFileRelDto : fileRelDtos) {
                if (tmpStoreOrderCartUpDto.getProductId().equals(tmpFileRelDto.getObjId())) {
                    url = imgUrl + tmpFileRelDto.getFileRealName();
                    tmpStoreOrderCartUpDto.setCoverPhoto(url);
                }
            }
        }

        return storeOrderCartUpDtos;
    }
}
